<?php

namespace App\Http\Controllers\Admin;

use Cartalyst\Sentry\Facades\Laravel\Sentry;
use Illuminate\Http\Request;
use App\Http\Requests;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Input;


require_once 'resources/org/code/Code.class.php';
class LoginController extends CommonController
{
    /**
     * 登录页面
     */
    public function login(Request $request){
        if($request->isMethod('post')){
        	$input=Input::all();
            //验证码
            $code=new \Code;
            $_code=$code->get();
            if(strtoupper($input['code'])!=$_code){
                return back()->with('errors','验证码错误!!');
            }
            //Sentry登录检测
            $cred = [
                'email'=>Input::get('email'),
                'password'=>Input::get('password'),
            ];

            try{
                $user = Sentry::authenticate($cred,false);
				
                if($user){
                    return redirect('admin');
                }else{
                    return back()->with('errors','网络错误请重试!');
                }
            }catch (\Exception $e){
                return back()->with('errors','用户名或者密码错误!');
            }
        }else{
        	$onUser = Sentry::getUser();
			if($onUser){
                return redirect('admin');
            }
            $title='登录';
            return view('admin.login',compact('title'));
        }
    }

    /**
     * 验证码
     */
    public function code(){
        $code=new \Code();
        $code->make();
    }

    /**
     * 退出系统
     */
    public function quit(){
        Sentry::logout();
        return redirect('admin/login');
    }
}
